package com.yunbiao.ybbusstopplate.doublescreen;

import android.app.Presentation;
import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yunbiao.ybbusstopplate.BuildConfig;
import com.yunbiao.ybbusstopplate.R;
import com.yunbiao.ybbusstopplate.utils.SpTool;
import com.yunbiao.ybbusstopplate.utils.eventbus.EventMessage;
import com.yunbiao.ybbusstopplate.utils.eventbus.EventTAG;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class SecondaryPresentation extends Presentation {
    public static final String TAG = "SecondaryPresentation";
    private Context context;
    private View fl_advert;
    private FrameLayout fl_advert_show;
    private TextView tv_line_left;
    private TextView tv_line_right;
    private TextView tv_lineshow_name;
    private FrameLayout fl_line_show;
    private View ll_noset_tip;
    private View fl_config;

    public SecondaryPresentation(Context outerContext, Display display) {
        this(outerContext, display,0);
    }

    public SecondaryPresentation(Context outerContext, Display display, int theme) {
        super(outerContext, display, theme);
        this.context = outerContext;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        fl_advert_show = findViewById(R.id.fl_advert_show);
        fl_advert = findViewById(R.id.fl_advert);
        tv_line_left = findViewById(R.id.tv_line_left);
        tv_line_right = findViewById(R.id.tv_line_right);
        tv_lineshow_name = findViewById(R.id.tv_lineshow_name);
        fl_line_show = findViewById(R.id.fl_line_show);
        ll_noset_tip = findViewById(R.id.ll_noset_tip);
        fl_config = findViewById(R.id.fl_config);

        Log.d(TAG, "onCreate: --------------------- ");
        initData();

        test();
    }

    private void test(){
        if (!BuildConfig.DEBUG) {
            return;
        }
//        findViewById(R.id.btn1).setOnClickListener(v -> {
////            LSPortAgreement.getInstance().testSiteList();
//            File file = new File(Environment.getExternalStorageDirectory(),"1.mp4");
//            UiMessageUtils.getInstance().send(AdvertView.VIDEO,file.getPath());
//        });
//        findViewById(R.id.btn2).setOnClickListener(v -> {
////            LSPortAgreement.getInstance().testNext();
//            File file = new File(Environment.getExternalStorageDirectory(),"1.png");
//            UiMessageUtils.getInstance().send(AdvertView.IMAGE,file.getPath());
//        });
//        findViewById(R.id.btn3).setOnClickListener(v -> {
//
//        });
    }

    private void initData(){
        EventBus.getDefault().register(this);

        int configSerialPortType = SpTool.Companion.getConfigSerialPortType();
        if(configSerialPortType == -1){
            ll_noset_tip.setVisibility(View.VISIBLE);
        } else {
            boolean configShowAdvert = SpTool.Companion.getConfigShowAdvert();
            int rotation = getDisplay().getRotation();
            if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180){
                configShowAdvert = true;
            }
            if(configShowAdvert){
                fl_advert.setVisibility(View.VISIBLE);
                String line_name = SpTool.Companion.get_line_name();
                if(!TextUtils.isEmpty(line_name)){
                    tv_line_left.setVisibility(View.VISIBLE);
                    tv_line_right.setVisibility(View.VISIBLE);
                    if(line_name.contains("路")){
                        line_name = line_name.replace("路","");
                        line_name += "路";
                    }
                    tv_lineshow_name.setText(line_name);
                }
            } else {
                fl_advert.setVisibility(View.GONE);
            }

            fl_line_show.postDelayed(this::showLineView,300);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: ---------------");
        playAdvert();
    }

    private void playAdvert(){
        if(!SpTool.Companion.getConfigShowAdvert()){
            fl_advert.setVisibility(View.GONE);
            return;
        }

        fl_advert.setVisibility(View.VISIBLE);
        clearAdvert();

//        fl_advert_show.setBackgroundColor(Color.WHITE);
        AdvertView advertView = new AdvertView(getContext());
        fl_advert_show.addView(advertView, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
    }

    private void clearAdvert(){
        int childCount = fl_advert_show.getChildCount();
        if (childCount > 0) {
            fl_advert_show.removeAllViews();
        }
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventMessage eventMessage){
        switch (eventMessage.getMsgTag()) {
            case EventTAG.EVENT_LINEINFO:
                if(tv_lineshow_name.getVisibility() == View.GONE){
                    return;
                }
                String[] lineInfo = (String[]) eventMessage.getMsgData();
                String lineName = lineInfo[0];
                if(TextUtils.isEmpty(lineName)){
                    tv_line_left.setVisibility(View.GONE);
                    tv_line_right.setVisibility(View.GONE);
                    tv_lineshow_name.setText(getContext().getString(R.string.no_line_tip));
                } else {
                    tv_line_left.setVisibility(View.INVISIBLE);
                    tv_line_right.setVisibility(View.VISIBLE);
                    if(lineName.contains("路")){
                        lineName = lineName.replace("路","");
                        lineName += "路";
                    }
                    tv_lineshow_name.setText(lineName);
                }
                break;
            case EventTAG.event_config_show:
                break;
            case EventTAG.event_config_dismiss:
                break;
            case EventTAG.event_config_update:
                showLineView();
                break;
        }
    }

    private BusStopPlateView busStopPlateView;
    private void showLineView(){
        fl_config.setVisibility(View.INVISIBLE);
        ll_noset_tip.setVisibility(View.INVISIBLE);
        if(busStopPlateView == null){
            busStopPlateView = new BusStopPlateView(getContext(),getDisplay());
            fl_line_show.addView(busStopPlateView.getRootView(), FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        }
    }


    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop: ---------------");
    }

    public static Display getSecondaryDisplay(Context context){
        DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = displayManager.getDisplays();
        if(displays == null || displays.length < 2){
            return null;
        }
        return displays[1];
    }
}
